<?php

/**
 * uniBoard Files Functions
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-06 01:45:14 +0000 (Mon, 06 Sep 2010) $
 * $Revision: 59 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class class_files{
	
	/**
	 * If file exist, delete it
	 *
	 * @param string $file_name
	 */
	
	static function unlink( $file_name){
		
		if ( file_exists( $file_name) && !is_dir( $file_name))
		{
			unlink( $file_name);
		}
		
	}
	
	// ------------------------------------------------------------------------
	
	/**
	 * This function creates new empty dir and puts new index.html file into it
	 *
	 * @param string $directory_name
	 */
	
	static function newDir( $directory_name){
		
		// File exists?
		if ( !file_exists( $directory_name . '/index.html'))
		{
			// Create new directory
			mkdir( $directory_name);
			
			// Put in new file
			file_put_contents( $directory_name . '/index.html', 'This directory is not available for browsing.');
		}
		
	}
			
	// ------------------------------------------------------------------------
	
	/**
	 * This function deletes directory
	 *
	 * @param string $directory_name
	 */
	
	static function deleteDir( $directory_name){
		
		// Is it an directory?
		if ( is_dir( $directory_name) && USE_GLOB)
		{
			// Anything inside dir?
			$dir_contents = glob( $directory_name . '/*');
			
			// Reiterate contents
			foreach ( $dir_contents as $item)
			{
				// Item a file?
				if ( is_dir( $item))
				{
					// Recure directory
					self::deleteDir( $item);
				}
				else
				{
					unlink( $item);
				}
			}
			
			// Delete directory
			rmdir( $directory_name);
		}
		
	}
	
}